<?php

namespace app\common\util;

use app\common\model\Member;
use app\common\services\LoginService;
use app\common\util\weixin\OAuth;
use think\Session;

class LoginManager
{
    private static function isFromWeiXin()
    {
        return ccnn_is_weixin_request();
    }

    public static function init()
    {
        if (!self::isFromWeiXin()) {
            LoginService::setAutoCheck(false);
            LoginService::registerLoginUrl(U('index/index'));
            LoginService::registerSessionKey(self::$_login_user_key);
        }
    }

    private static $_login_user_key = 'login_user_key';

    public static function setLoginUser($user)
    {
        LoginService::setLoginUser($user);
    }

    public static function getLoginUserInfo($field = null)
    {
        if (!self::isFromWeiXin()) {
            return LoginService::getLoginUser($field);
        }
        $user = session(self::$_login_user_key);
        if (empty($user)) {
            $userInfo = OAuth::getUserInfo();
            $user = Member::instance()->where(['openid' => $userInfo['openid']])->find();
            if (empty($user)) {
                $user =[
                    'openid' => $userInfo['openid'],
                    'sex' => $userInfo['sex'],
                    'nickname' => $userInfo['nickname'],
                    'avatar' => $userInfo['headimgurl'],
                    'create_time'=>DateTimeUtil::now()
                ];
                $user['id'] = Member::instance()->add($user);
            } else {
                $user = ['openid' => $user['openid'],
                    'nickname' => $user['nickname'],
                    'sex' => $user['sex'],
                    'avatar' => $user['avatar'],
                    'id' => $user['id']];
            }
            session(self::$_login_user_key, $user);
        }
        if (!empty($field)) {
            return $user[$field];
        }
        return $user;
    }

    public static function getLoginUserId()
    {
        return self::getLoginUserInfo('id');
    }

    public static function logOff()
    {
        $user = session(self::$_login_user_key);
        if (!empty($user)) {
            if (!empty($user['account_type']) && $user['account_type'] == 1) { // for sina
                $access_token = $user['access_token'];
                if (!empty($access_token)) {
                    CUrl::httpGet('https://api.weibo.com/oauth2/revokeoauth2', ['access_token' => $access_token]);
                }
            }
            Session::delete(self::$_login_user_key);
        }
    }

    public static function doCheckLogin()
    {
        if (!self::isLoginOn()) {
            if (self::isFromWeiXin()) {
                self::getLoginUserId();
            } else {
                LoginService::redirectToLogin();
            }
        }
    }

    public static function isLoginOn()
    {
        return empty(session(self::$_login_user_key)) ? false : true;
    }

    public static function hasSubscribe()
    {
        return empty(self::getLoginUserInfo('subscribe')) ? false : true;
    }
}

LoginManager::init();



